#!/bin/sh 
#

if [ $# -ne 6 ]; then
    echo "$0: incorrect number of input arguments" 
    exit 1
fi

optfile=${1%^*}
nlfile=${1#*^}
info=$2
res=${3%^*}
land_res=${3#*^}
mask=${4%@*}
sim_year=${4#*@}
config_file=$5
start_type=$6

cfgdir=${CLM_SCRIPTDIR}/../../bld

if [ "$optfile" = "$1" ]; then
   nlfile="nl_std"
fi
if [ "$mask" = "$4" ]; then
   sim_year="default"
fi
if [ "$res" = "$3" ]; then
   land_res="default"
fi

start_ymd=`echo $info | awk -F: '{print $1}'`
dtime=`echo     $info | awk -F: '{print $2}'`
if   [ $run_length -ge -2  ] && [ $run_length -le 48   ]; then
   hist_nhtfrq="3,1"
elif [ $run_length -gt -30 ] && [ $run_length -lt 1440 ]; then
   hist_nhtfrq="-24,-8"
else
   hist_nhtfrq="0,-240"
fi

if [ $run_length -lt 0  ]; then
  length=`expr 0 - $run_length`
  runlen="${length}d"
else
  runlen="${run_length}s"
fi
#
# Initialize the following so won't do anything by default
#
restart_file=" "
restfilm=" "
restfils=" "
nrevsnfile=" "
finidfile=" "
logfile=" "
atm_cpl_dt=" "
#
# If branch and a sequential-CCSM simulation -- set restart files using
#
if [ "$6" = "branch" ]; then
  if [ -f "$drv_restart" ]; then
     restart_file="restart_file='$drv_restart'"
  fi 
  nrevsnfile=" nrevsn='$nrevsn'"
  if [ -f "$datm_restfilm" ]; then
     restfilm="restfilm='$datm_restfilm'"
  fi 
  if [ -f "$datm_restfils" ]; then
     restfils="restfils='$datm_restfils'"
  fi 
fi
#
# For startup if an input if an initial file exists use it
#
if [ "$6" = "startup" ]; then
  if [ -f "$finidat" ]; then
      finidfile="finidat='$finidat'"
  fi
fi
#
# For sequential-CCSM -- always do the following...
#
logfile="logfilepostfix='.test.log'"
atm_cpl_dt="atm_cpl_dt=$dtime"

#
# If pergro -- don't set history frequency
#
pergro=`grep 'id="pergro"' $config_file | awk '{print $3}'`
if [ "$pergro" = 'value="on"' ]; then
  echo "pergro is on"
  hist_freq=" "
  hist_samp=" "
else
  hist_freq=" hist_nhtfrq=$hist_nhtfrq "
  hist_samp=" hist_mfilt=1,3"
fi


clmopt=`cat ${CLM_SCRIPTDIR}/nl_files/$optfile`
clmnl=${CLM_SCRIPTDIR}/nl_files/$nlfile

datm_source=`grep datm_source ${CLM_SCRIPTDIR}/nl_files/$optfile | awk '{print $NF}'`
if [ "$datm_source" = "CLM_QIAN" ]; then
   clmopt="$clmopt -datm_data_dir $DATM_QIAN_DATA_DIR"
fi


echo "env OMP_NUM_THREADS=$CLM_THREADS $cfgdir/build-namelist -mask $mask -sim_year $sim_year -config $config_file \ "
echo "                 -infile $clmnl -res $res -lnd_res $land_res -clm_start_type $6 \ "
echo "                 -csmdata $CSMDATA -drv_runlength $runlen $clmopt -test \ "
echo "                 -namelist \"&seq_timemgr_inparm start_ymd=$start_ymd $atm_cpl_dt / "
echo "                             &seq_infodata_inparm $restart_file $logfile / "
echo "                             &datm_nml $restfilm $restfils / "
echo "                             &clm_inparm dtime=$dtime "
echo "                             $hist_samp $nrevsnfile $hist_freq /\""

env OMP_NUM_THREADS=$CLM_THREADS $cfgdir/build-namelist -mask $mask -sim_year $sim_year -config $config_file \
                       -infile $clmnl -res $res -lnd_res $land_res -clm_start_type $6 \
                       -csmdata $CSMDATA -drv_runlength $runlen $clmopt -test \
                       -namelist "&seq_timemgr_inparm start_ymd=$start_ymd $atm_cpl_dt / \
                                  &seq_infodata_inparm $restart_file $logfile / \
                                  &datm_nml $restfilm $restfils / \
                                  &clm_inparm dtime=$dtime $hist_samp $nrevsnfile $finidfile $hist_freq /"
